#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vector>
//#include "emis.h"
#include "manager.h"
#include "managerdao_file_impl.h"

using namespace std;
vector <Manager> managers;
void ManagerDaoFileImpl::load(void)
{
	int fd = open("manager.dat",O_RDONLY|O_CREAT,0644);
	int ret;
	do
	{
		ret = 0;
		int id;
		char name[20];
		char password[20];
		ret += read(fd,&id,sizeof(id));
		ret += read(fd,&name,sizeof(name));
		ret += read(fd,&password,sizeof(password));
		if(0 < ret)
		{
			Manager manager(id,name,password);
			managers.push_back(manager);
		}
	}while(ret != 0);
	close(fd);
	return;
}

void ManagerDaoFileImpl::save(void)
{
	int fd = open("manager.dat",O_WRONLY|O_TRUNC,0644);
	if(0 > fd)
	{
		return;
	}
	for(unsigned int i=0;i<managers.size();i++)
	{
		write(fd,&managers[i].id,sizeof(managers[i].id));
		write(fd,&managers[i].name,sizeof(managers[i].name));
		write(fd,&managers[i].password,sizeof(managers[i].password));
	}
	close(fd);
	return;
}
